from lxml import etree
import requests

def get_stockcode():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        '(KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
    }
    url1 = 'http://data.10jqka.com.cn/funds/ggzjl/field/code/order/asc/page/'
    url2 = '/ajax/1/free/1/'

    stockcode = []
    for page in range(1,75):
        url = url1 + str(page) + url2
        res = requests.get(url, headers=headers)
        if res.status_code == 200:
            html = etree.HTML(res.text) # parse the html
            stockcode += html.xpath('//a[@class="stockCode"]/text()') # 利用xpath寻找stockcode
    print(len(stockcode))
    
    def save_txt(name, content):
        with open(name, 'w', encoding='utf-8') as f:
            f.write(content)
    
    content_str = ' '.join(stockcode) # 将list型的stockcode用空格分割，转换为str类型
    save_txt('stockcode.txt', content_str) # 存储到txt

if __name__ == '__main__':
    get_stockcode()